<?php
include_once('root.inc.php');
include_once($ROOT.'base.inc.php');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
//mb_internal_encoding('UTF-8');

//load teh base level user information
$user = new User();
//$result = UserManager::loginUser( $user );
/*if( !validateuser( $result ) ){
	die ("Fialed to login user");
}*/
try {
	if(empty($_GET["action"])) {
		throw new Exception("Invalid GET request. Missing action code.");
	}
	switch( $_GET["action"] ){
		case "bigBoard":
			if( empty( $_GET["format"] ) ) {
				throw new Exception("format is required.");
			}
			$format = $_GET["format"];
			$feeds = array();
				
			$feed1 = new FeedSource();
			$feed1->url = 'http://feeds.feedburner.com/surfline-rss-surf-news';
			$feed1->type = FeedSource::RSS_SOURCE;
			array_push( $feeds, $feed1 );
			
			$feed2 = new FeedSource();
			$feed2->url = 'http://gdata.youtube.com/feeds/api/users/aspworldtour/uploads';
			$feed2->type = FeedSource::RSS_SOURCE;
			array_push( $feeds, $feed2 );
			
			$feed3 = new FeedSource();
			$feed3->url = 'http://www.aspworldtour.com/feed/';
			$feed3->type = FeedSource::RSS_SOURCE;
			array_push( $feeds, $feed3 );
			
			$feed4 = new FeedSource();
			$feed4->url = 'http://twitter.com/users/show_for_profile.json?screen_name=fantasysurfer';
			$feed4->type = FeedSource::TWITTER_SOURCE;
			array_push( $feeds, $feed4 );
			
			$feed5 = new FeedSource();
			$feed5->url = 'http://api.twitter.com/1/statuses/following_timeline.json?include_entities=1&include_available_features=1&contributor_details=true&user_id=22449367';
			$feed5->type = FeedSource::TWITTER_SOURCE;
			array_push( $feeds, $feed5 );
			
			$boardItems = UserManager::getBoardItems( $user, $feeds );
			
			if( $format == 'JSON' ){
				header('Content-type: application/json; charset=utf-8');
				echo (string)str_replace( array( "\r", "\r\n", "\n" ), '', json_encode( $boardItems ) );
			}elseif( $format == 'HTML' ){
				header('Content-type: text/html; charset=utf-8');
				$htmlOut = "";
				foreach( $boardItems as $boardItem ){
					if( $boardItem->type == BoardItem::RSS ){
						$htmlOut .= "<div><a href='". $boardItem->url ."'><h1>". $boardItem->title ."</h1></a><p>".$boardItem->text ."</div>";
						if( $boardItem->enclosure ){
							$htmlOut .= "<div>" . $boardItem->enclosure ."</div>";
						}
					}elseif( $boardItem->type == BoardItem::TWITTER ){
						$htmlOut .= "<p>". date('M d, Y G:i:s', $boardItem->date ) ."</p>";
						$htmlOut .= "<p><a href='". $boardItem->url ."'><img src='". $boardItem->image ."'/></a>" . $boardItem->source . ": " . $boardItem->text . "<p>";
						if( $boardItem->enclosure ){
							foreach( $boardItem->enclosure as $enclosure){
								$htmlOut .= "<img src='" . $enclosure->url ."' width='". $enclosure->width ."' height='". $enclosure->height ."' /></br>";
							}
						}
					}
				}
				echo $htmlOut;
			}
			break;
		case "updateUser":
			if( empty( $_POST["user"] ) ) {
				throw new Exception("Invalid POST request. User is required.");
			}
			header('Content-type: application/json; charset=utf-8');
			$temp = stripslashes($_POST["user"]);
			$obj = json_decode( $temp );
			$user = new User();
			$user->fbID = $obj->{'fbID'};
			$user->fbToken = $obj->{'fbToken'};
			$user->email = $obj->{'email'};
			$user->fbTokenExpiresOn = $obj->{'fbTokenExpiresOn'};
			$user->firstName = $obj->{'firstName'};
			$user->lastName = $obj->{'lastName'};
			$user->timezone = $obj->{'timezone'};
			$role = new Role( null, "Site User", null, array("updateUser", "deleteUser","readUser") );
			$user->roles["Site User"] = $role;
			//for testing only, delete the admin role in production
			$role = new Role( null, "Admin", null, array("updateRole", "listDomains","deleteRole") );
			$user->roles["Admin"] = $role;
			try{
				$response = UserManager::updateUser( $user );
			}catch( Exception $e ){
				$response = new SrfBetError( null, $e->getMessage() . "\r\n" . $e->getTraceAsString() );
			}
			if( get_class( $response ) != "SrfBetError" ){
				$output = $user;
			}else{
				$output = new stdClass();
				$output->error = $response;
				header("HTTP/1.0 500 Internal Server Error");
			}
			echo (string)str_replace( array( "\r", "\r\n", "\n" ), '', json_encode( $output ) );
			//echo var_dump( UserManager::updateRole( $user, $role ) );
			//echo (string)str_replace(array("\r", "\r\n", "\n"), '', UserManager::updateRole( $user, $role ));
			break;
		case "updateRole":
			if( empty( $_POST["role"] ) ) {
				throw new Exception("Invalid POST request. Role is required.");
			}
			if( empty( $_GET["fbID"] ) ) {
				throw new Exception("Invalid GET request. fbID is required.");
			}
			header('Content-type: application/json; charset=utf-8');
			$user = new User();
			$user->fbID = $_GET["fbID"];
			//get the users account details
			try{
				$response = UserManager::loginUser( $user );
			}catch( Exception $e ){
				$response = new SrfBetError( null, $e->getMessage() . "\r\n" . $e->getTraceAsString() );
			}
			if( get_class( $response ) != "SrfBetError" && validateuser( $response ) ){
				$user = $response["user"];
				$temp = stripslashes($_POST["role"]);
				$obj = json_decode( $temp );
				$role = new Role();
				$role->name = $obj->{'name'};
				$role->keys = $obj->{'keys'};
				try{
					$response = UserManager::updateRole( $user, $role );
				}catch( Exception $e ){
					$response = new SrfBetError( null, $e->getMessage() . "\r\n" . $e->getTraceAsString() );
				}
			}else{
				$response = new SrfBetError( null, "Invalid user" );
			}
			if( get_class( $response ) != "SrfBetError" ){
				$output = $response;
			}else{
				$output = new stdClass();
				$output->error = $response;
				header("HTTP/1.0 500 Internal Server Error");
			}
			echo (string)str_replace( array( "\r", "\r\n", "\n" ), '', json_encode( $output ) );
			//echo var_dump( UserManager::updateRole( $user, $role ) );
			//echo (string)str_replace(array("\r", "\r\n", "\n"), '', UserManager::updateRole( $user, $role ));
			break;
		case "updateDomain":
			if( empty( $_POST["domain"] ) ) {
				throw new Exception("Invalid POST request. Domain is required.");
			}
			if( empty( $_GET["fbID"] ) ) {
				throw new Exception("Invalid GET request. fbID is required.");
			}
			header('Content-type: application/json; charset=utf-8');
			$user = new User();
			$user->fbID = $_GET["fbID"];
			//get the users account details
			try{
				$response = UserManager::loginUser( $user );
			}catch( Exception $e ){
				$response = new SrfBetError( null, $e->getMessage() . "\r\n" . $e->getTraceAsString() );
			}
			if( get_class( $response ) != "SrfBetError" && validateuser( $response ) ){
				$user = $response["user"];
				$temp = stripslashes($_POST["domain"]);
				$obj = json_decode( $temp );
				$domain = new Domain();
				$domain->name = $obj->{'name'};
				try{
					$response = UserManager::updateDomain( $user, $domain );
				}catch( Exception $e ){
					$response = new SrfBetError( null, $e->getMessage() . "\r\n" . $e->getTraceAsString() );
				}
			}else{
				$response = new SrfBetError( null, "Invalid user" );
			}
			if( get_class( $response ) != "SrfBetError" ){
				$output = $response;
			}else{
				$output = new stdClass();
				$output->error = $response;
				header("HTTP/1.0 500 Internal Server Error");
			}
			echo (string)str_replace( array( "\r", "\r\n", "\n" ), '', json_encode( $output ) );
			//echo var_dump( UserManager::updateRole( $user, $role ) );
			//echo (string)str_replace(array("\r", "\r\n", "\n"), '', UserManager::updateRole( $user, $role ));
			break;
		case "listDomains":
			if( empty( $_GET["fbID"] ) ) {
				throw new Exception("Invalid GET request. fbID is required.");
			}
			header('Content-type: application/json; charset=utf-8');
			$user = new User();
			$user->fbID = $_GET["fbID"];
			//get the users account details
			try{
				$response = UserManager::loginUser( $user );
			}catch( Exception $e ){
				$response = new SrfBetError( null, $e->getMessage() . "\r\n" . $e->getTraceAsString() );
			}
			if( get_class( $response ) != "SrfBetError" && validateuser( $response ) ){
				$user = $response["user"];
				try{
					$response = UserManager::listDomains( $user );
				}catch( Exception $e ){
					$response = new SrfBetError( null, $e->getMessage() . "\r\n" . $e->getTraceAsString() );
				}
			}else{
				$response = new SrfBetError( null, "Invalid user" );
			}
			if( get_class( $response ) != "SrfBetError" ){
				$output = $response;
			}else{
				$output = new stdClass();
				$output->error = $response;
				header("HTTP/1.0 500 Internal Server Error");
			}
			echo (string)str_replace( array( "\r", "\r\n", "\n" ), '', json_encode( $output ) );
			//echo var_dump( UserManager::updateRole( $user, $role ) );
			//echo (string)str_replace(array("\r", "\r\n", "\n"), '', UserManager::updateRole( $user, $role ));
			break;
		case "getRoles":
			if( empty( $_GET["fbID"] ) ) {
				throw new Exception("Invalid GET request. fbID is required.");
			}
			header('Content-type: application/json; charset=utf-8');
			$user = new User();
			$user->fbID = $_GET["fbID"];
			//get the users account details
			try{
				$response = UserManager::loginUser( $user );
			}catch( Exception $e ){
				$response = new SrfBetError( null, $e->getMessage() . "\r\n" . $e->getTraceAsString() );
			}
			if( get_class( $response ) != "SrfBetError" && validateuser( $response ) ){
				$user = $response["user"];
				try{
					$response = UserManager::getRoles( $user );
				}catch( Exception $e ){
					$response = new SrfBetError( null, $e->getMessage() . "\r\n" . $e->getTraceAsString() );
				}
			}else{
				$response = new SrfBetError( null, "Invalid user" );
			}
			if( get_class( $response ) != "SrfBetError" ){
				$output = $response;
			}else{
				$output = new stdClass();
				$output->error = $response;
				header("HTTP/1.0 500 Internal Server Error");
			}
			echo (string)str_replace( array( "\r", "\r\n", "\n" ), '', json_encode( $output ) );
			break;
		case "getUsers":
			if( empty( $_GET["fbID"] ) ) {
				throw new Exception("Invalid GET request. fbID is required.");
			}
			header('Content-type: application/json; charset=utf-8');
			$user = new User();
			$user->fbID = $_GET["fbID"];
			//get the users account details
			try{
				$response = UserManager::loginUser( $user );
			}catch( Exception $e ){
				$response = new SrfBetError( null, $e->getMessage() . "\r\n" . $e->getTraceAsString() );
			}
			if( get_class( $response ) != "SrfBetError" && validateuser( $response ) ){
				$user = $response["user"];
				try{
					$response = UserManager::getAllUsers( $user );
				}catch( Exception $e ){
					$response = new SrfBetError( null, $e->getMessage() . "\r\n" . $e->getTraceAsString() );
				}
			}else{
				$response = new SrfBetError( null, "Invalid user" );
			}
			if( get_class( $response ) != "SrfBetError" ){
				$output = $response;
			}else{
				$output = new stdClass();
				$output->error = $response;
				header("HTTP/1.0 500 Internal Server Error");
			}
			echo (string)str_replace( array( "\r", "\r\n", "\n" ), '', json_encode( $output ) );
			break;
		default:
			header("HTTP/1.0 501 Not Implemented");
	}
} 
catch (Exception $ex) {
	print_r($ex);
}
function validateuser( $result ){
	if( $result['loginStatus'] == true ){
		return true;
		
	}else{
		return false;
	}
}
?>